package joda;

import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.Duration;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

public class JodaTime {

	public static void main(String[] args) {
		// 生成1个当前时间对象
		DateTime dt1 = new DateTime();
		System.out.println("dt1 : " + dt1);
		
		// 生成指定时间对象
		DateTime dt2 = new DateTime(2018, 6, 20, 10, 23, 58);
		System.out.println("dt2 : " + dt2);
		
		// 通过字符串生成时间对象
		DateTime dt3 = new DateTime("2018-06-19T19:00:09");
		System.out.println("dt3 : " + dt3);
		
		// 只需要年、月、日
		LocalDate dt4 = new LocalDate(2018, 6, 19);
		System.out.println("dt4 : " + dt4);
		
		// 只需要时、分、秒、毫秒
		LocalTime lt1 = new LocalTime(10, 22, 33, 0);
		System.out.println("lt1 : " + lt1);
		
		// 时间DateTime的年、月、日、星期、时、分、秒、毫秒
		DateTime dt = new DateTime(new Date());
		System.out.println("dt :" + dt);
		// 年
		int year = dt.getYear();
		System.out.println("year : " + year);
		// 月
		int monthOfYear = dt.getMonthOfYear();
		System.out.println("monthOfYear : " + monthOfYear);
		// 日
		int dayOfMonth = dt.getDayOfMonth();
		System.out.println("dayOfMonth : " + dayOfMonth);
		// 周几
		int dayOfWeek = dt.getDayOfWeek();
		System.out.println("dayOfWeek : " + dayOfWeek);
		// 一年中第几周
		int weekOfYear = dt.getWeekOfWeekyear();
		System.out.println("weekOfYear : " + weekOfYear);
		// 一年中第几天
		int dayOfYear = dt.getDayOfYear();
		System.out.println("dayOfYear : " + dayOfYear);
		// 时
		int hourOfDay = dt.getHourOfDay();
		System.out.println("hourOfDay : " + hourOfDay);
		// 分
		int miniteOfHour = dt.getMinuteOfHour();
		System.out.println("miniteOfHour : " + miniteOfHour);
		// 秒
		int secondOfMinite = dt.getSecondOfMinute();
		System.out.println("secondOfMinite : " + secondOfMinite);
		// 1天中第几分钟
		int miniteOfDay = dt.getMinuteOfDay();
		System.out.println("miniteOfDay : " + miniteOfDay);
		// 1天中第几秒
		int secondOfDay = dt.getSecondOfDay();
		System.out.println("secondOfDay : " + secondOfDay);
		// 毫秒
		int millOfSecond = dt.getMillisOfSecond();
		System.out.println("millOfSecond : " + millOfSecond);
		
		// 时间常量
		System.out.println(DateTimeConstants.JANUARY);
		System.out.println(DateTimeConstants.DECEMBER);
		System.out.println(DateTimeConstants.MONDAY);
		System.out.println(DateTimeConstants.SUNDAY);
		
		// 日期加减
		// 昨天
		DateTime yesterday = dt.minusDays(1);
		System.out.println("yesterday :" + yesterday);
		// 明天
		DateTime tomorrow = dt.plusDays(1);
		System.out.println("tomorrow :" + tomorrow);
		// 3个月之前
		DateTime threeMonthAgo = dt.minusMonths(3);
		System.out.println("threeMonthAgo :" + threeMonthAgo);
		// 1个月以后
		DateTime oneMonthAfter = dt.plusMonths(1);
		System.out.println("oneMonthAfter :" + oneMonthAfter);
		// 2年前
		DateTime twoYearsAgo = dt.minusYears(2);
		System.out.println("twoYearsAgo :" + twoYearsAgo);
		// 1年后
		DateTime oneYearLater = dt.plusYears(1);
		System.out.println("oneYearLater :" + oneYearLater);
		
		// 获取特殊日期
		// 本月最后1天
		DateTime lastDayOfMonth = dt.dayOfMonth().withMaximumValue();
		System.out.println("lastDayOfMonth : " + lastDayOfMonth);
		// 本周最后1天
		DateTime lastDayOfWeek = dt.dayOfWeek().withMaximumValue();
		System.out.println("lastDayOfWeek : " + lastDayOfWeek);
		// 下周第1天
		DateTime firstDayOfNextWeek = dt.plusDays(7).dayOfWeek().withMinimumValue();
		System.out.println("firstDayOfNextWeek : " + firstDayOfNextWeek);
		// 90天后的那一周的周一
		DateTime firstDayOf90DaysLater = dt.plusDays(90).dayOfWeek().withMinimumValue();
		System.out.println("firstDayOf90DaysLater : " + firstDayOf90DaysLater);
		
		// 计算时间差
		DateTime begin = new DateTime("2018-06-01");
		DateTime end = new DateTime("2018-06-19");
		Period p = new Period(begin, end, PeriodType.days());
		int daysInterval = p.getDays();
		System.out.println("daysInterval : " + daysInterval);

		DateTime begin1 = new DateTime("2018-06-19T13:34:31");
		DateTime end1 = new DateTime("2018-06-19T14:34:31");
		Duration d = new Duration(begin1, end1);
		long mills = d.getMillis();
		System.out.println("mills : " + mills);
		long seconds = mills / 1000;
		System.out.println("seconds : " + seconds);
		
		// 日期比较
		DateTime dateTime1 = new DateTime("2018-06-01");
		DateTime dateTime2 = new DateTime("2018-06-11");
		boolean f1 = dateTime1.isAfter(dateTime2);
		System.out.println(f1);
		boolean f2 = dateTime1.isBefore(dateTime2);
		System.out.println(f2);
		boolean f3 = dateTime1.isEqualNow();
		System.out.println(f3);
		
		// 格式化输出
		DateTime dateTime = new DateTime();
		String s1 = dateTime.toString("yyyy/MM/dd hh:mm:ss");
		System.out.println(s1);
		String s2 = dateTime.toString("yyyy/MM/dd hh:mm:ss.SSSa");
		System.out.println(s2);
		String s3 = dateTime.toString("yyyy-MM-dd hh:mm:ss");
		System.out.println(s3);
		String s4 = dateTime.toString("yyyy-MM-dd");
		System.out.println(s4);
		String s5 = dateTime.toString("yyyy-MM-dd HH:mm Z");
		System.out.println(s5);
	}

}
